﻿<UserControl x:Class="DaxStudio.UI.Views.QueryPlanTraceView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:conv="clr-namespace:DaxStudio.UI.Converters"
             xmlns:behaviours="clr-namespace:DaxStudio.UI.Behaviours"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="600" BorderThickness="1" BorderBrush="DimGray">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Resources/IconResourceDictionary.xaml" />
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Styles/LabelSpinnerStyle.xaml" />
                <ResourceDictionary  Source="pack://application:,,,/DaxStudio.UI;Component/Styles/BusyPanel.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <conv:BoolToHiddenConverter x:Key="BoolToVisibilityConverter" />
        </ResourceDictionary>
        
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0" SelectedItem="{Binding SelectedPhysicalRow}" HorizontalAlignment="Stretch" AutoGenerateColumns="False"
                  behaviours:GridViewColumnResize.Enabled="True" VerticalAlignment="Stretch" Background="White" Width="Auto"
                  ItemsSource="{Binding PhysicalQueryPlanRows}" 
                  SelectionMode="Extended"
                  SelectionUnit="FullRow"
                  HeadersVisibility="Column"
                  FrozenColumnCount="1"
                  ClipboardCopyMode="IncludeHeader"
                  GridLinesVisibility="None" BorderBrush="#FFABADB3" >

            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                </Style>
                <Style x:Key="AlignRight" TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Right" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Padding" Value="5,0,5,0"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border Padding="{TemplateBinding Padding}" 
                                        BorderBrush="{TemplateBinding BorderBrush}" 
                                        BorderThickness="{TemplateBinding BorderThickness}" 
                                        Background="{TemplateBinding Background}" 
                                        SnapsToDevicePixels="True">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Line" Width="40" IsReadOnly="True" Binding="{Binding RowNumber, StringFormat='#,#;;'}" ElementStyle="{StaticResource AlignRight}" />
                <DataGridTextColumn Header="Records" IsReadOnly="True" Width="70" Binding="{Binding Records, StringFormat='#,0'}" ElementStyle="{StaticResource AlignRight}" />
                <DataGridTextColumn Header="Physical Query Plan" behaviours:GridViewColumnResize.Width="1*" 
                                    IsReadOnly="True" Binding="{Binding IndentedOperation}"/>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <EventSetter Event="Control.RequestBringIntoView" Handler="DataGrid_RequestBringIntoView"  />
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="Copy" Icon="{StaticResource CopyIcon}"></MenuItem>
                    <Separator/>
                    <MenuItem Command="SelectAll"></MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>
        <GridSplitter Grid.Row="1" Height="5"  ResizeDirection="Rows" 
            Grid.Column="0"
            Width="Auto" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" 
            Margin="0"/>
        <DataGrid Grid.Row="2" SelectedItem="{Binding SelectedLogicalRow}" HorizontalAlignment="Stretch" AutoGenerateColumns="False"
                  behaviours:GridViewColumnResize.Enabled="True" VerticalAlignment="Stretch" Background="White" Width="Auto"
                  ItemsSource="{Binding LogicalQueryPlanRows}" 
                  SelectionMode="Extended"
                  SelectionUnit="FullRow"
                  HeadersVisibility="Column"
                  FrozenColumnCount="1"
                  ClipboardCopyMode="IncludeHeader"
                  GridLinesVisibility="None" BorderBrush="#FFABADB3" >

            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                </Style>
                <Style x:Key="AlignRight" TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Right" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Padding" Value="5,0,5,0"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border Padding="{TemplateBinding Padding}" 
                                        BorderBrush="{TemplateBinding BorderBrush}" 
                                        BorderThickness="{TemplateBinding BorderThickness}" 
                                        Background="{TemplateBinding Background}" 
                                        SnapsToDevicePixels="True">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Line" Width="40" IsReadOnly="True" Binding="{Binding RowNumber, StringFormat='#,#;;'}" ElementStyle="{StaticResource AlignRight}" />
                <DataGridTextColumn Header="Logical Query Plan" behaviours:GridViewColumnResize.Width="1*" 
                                    IsReadOnly="True" Binding="{Binding IndentedOperation}"/>
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <EventSetter Event="Control.RequestBringIntoView" Handler="DataGrid_RequestBringIntoView"  />
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="Copy" Icon="{StaticResource CopyIcon}"></MenuItem>
                    <Separator/>
                    <MenuItem Command="SelectAll"></MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>

        <!-- Busy overlay -->
        <Border 
                Visibility="{Binding IsBusy, Converter={StaticResource BoolToVisibilityConverter}}" 
                Grid.RowSpan="3" Style="{StaticResource BusyPanel}">
            <Grid>

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <!--
                    <Views:ucSpinner Foreground="White" Margin="0,0,10,0" />
                    <TextBlock Margin="0" TextWrapping="Wrap" Text="Running Query..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontWeight="Bold" Foreground="#7E000000"/>
                    -->
                    <Label Style="{StaticResource LabelSpinnerStyle}" Height="30" VerticalContentAlignment="Center"  FontSize="18" FontWeight="Bold" Foreground="#D0000000">Running Query...</Label>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</UserControl>
